package net.huashitong.synthetical.TaskJob.dao;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.synthetical.TaskJob.Entity.TaskJob;
import net.huashitong.synthetical.TaskJob.filter.TaskJobFilter;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class TaskJobDao extends StringDao<TaskJob> {


    /**
     * 任务清单页面查询主入口
     * @param filter
     * @return
     */
    public List<TaskJob> getMissinTask(TaskJobFilter filter)
    {
        GenericQuery query = createQuery( "obj");
        if (filter.getTasktimes()!=null&&!"".equals(filter.getTasktimes()))
            query.append(" and taskTimes =:taskTimes").setParam("taskTimes",filter.getTasktimes());
        if (filter.getTaskName()!=null&&!"".equals(filter.getTaskName()))
            query.append(" and taskName like:taskName").setParam("taskName",
                    StringUtils.getLikewords(filter.getTaskName()));
        if (filter.getProjectName()!=null&&!"".equals(filter.getProjectName()))
            query.append(" and projectName like:projectName").setParam("projectName",
                    StringUtils.getLikewords(filter.getProjectName()));
        if (filter.getTaskContent()!=null&&!"".equals(filter.getTaskContent()))
            query.append(" and taskContent like:taskContent").setParam("taskContent",
                    StringUtils.getLikewords(filter.getTaskContent()));
        if (filter.getStartDate()!=null&&!"".equals(filter.getStartDate()))
            query.append(" and startDate =:startDate").setParam("startDate",filter.getStartDate());
        if (filter.getEndDate()!=null&&!"".equals(filter.getEndDate()))
            query.append(" and endDate =:endDate").setParam("endDate",filter.getEndDate());
        if (filter.getRespResult()!=null&&!"".equals(filter.getRespResult()))
            query.append(" and respResult =:respResult").setParam("respResult",filter.getRespResult());
        if (filter.getSupervisionResult()!=null&&!"".equals(filter.getSupervisionResult()))
            query.append(" and supervisionResult =:supervisionResult").setParam("supervisionResult",filter.getSupervisionResult());
        if (filter.getNodeId()!=null&&!"".equals(filter.getNodeId()))
            query.append(" and nodeId =:nodeId").setParam("nodeId",filter.getNodeId());
        if (filter.getNodeName()!=null&&!"".equals(filter.getNodeName()))
            query.append(" and nodeName like:nodeName").setParam("nodeName",
                    StringUtils.getLikewords(filter.getNodeName()));
        if (filter.getResDept()!=null&&!"".equals(filter.getResDept()))
            query.append(" and resDept like:resDept").setParam("resDept",
                    StringUtils.getLikewords(filter.getResDept()));
        if (filter.getResUser()!=null&&!"".equals(filter.getResUser()))
            query.append(" and resUser like:resUser").setParam("resUser",
                    StringUtils.getLikewords(filter.getResUser()));
        if (filter.getResPhone()!=null&&!"".equals(filter.getResPhone()))
            query.append(" and resPhone like:resPhone").setParam("resPhone",
                    StringUtils.getLikewords(filter.getResPhone()));
        if (filter.getOperatorUser()!=null&&!"".equals(filter.getOperatorUser()))
            query.append(" and operatorUser like:operatorUser").setParam("operatorUser",
                    StringUtils.getLikewords(filter.getOperatorUser()));
        return query.listResult(filter);
    }

    /**
     * 保存到督查系统
     */
    public void savetask()
    {
        ResultSet set  = null;
        try {
            Connection conn  = SessionFactoryUtils.getDataSource(sessionFactory).getConnection();
            String call = "{call tasktest() }";
            CallableStatement cs = conn.prepareCall(call);
            cs.execute();
            conn.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
